package com.cndym.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.type.TypeReference;

import com.cndym.utils.ConfigUtils;
import com.cndym.utils.HttpClientUtils;
import com.cndym.utils.JsonBinder;
import com.cndym.utils.Utils;

/**
 * post请求转发
 * 
 * @author chengwei
 *
 */
public class RedirectRequestFilter implements Filter {
	private Logger logger = Logger.getLogger(getClass());
	private static String url = ConfigUtils.getValue("OLD_API_URL");
	public static Map<String, String> cmdMaping = new HashMap<String, String>();
	public static Map<String, String> uriMaping = new HashMap<String, String>();
	public static Map<String, String> cmdFuncMaping = new HashMap<String, String>();

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String msg = Utils.formatStr(request.getParameter("msg"));

		Map<String, String> paraMap = new HashMap<String, String>();

		try {
			paraMap = JsonBinder.buildNormalBinder().getMapper().readValue(msg,
					new TypeReference<Map<String, String>>() {
					});
			String cmd = paraMap.get("cmd");
			String func = paraMap.get("func");
			String uri = request.getRequestURI();
			// 根据cmd,func或uri做转发
			if (cmdMaping.get(cmd) == null && uriMaping.get(uri) == null
					&& cmdFuncMaping.get(cmd + "_" + func) == null) {
				logger.info("符合转发规则,cmd=" + cmd + ",func" + func + " or uri=" + uri);
				Map<String, String> para = new HashMap<String, String>();
				para.put("msg", msg);
				HttpClientUtils hcu = new HttpClientUtils(url, "utf-8", para);
				String reStr = hcu.httpClientRequest(-1, -1);
				resp.getWriter().print(reStr);
				return;
			}
		} catch (Exception ex) {
			logger.error("", ex);
		}
		chain.doFilter(request, response);
	}

	static {
		cmdMaping.put("3100", "注册接口");
		cmdMaping.put("3101", "登录接口");
		cmdMaping.put("3102", "用户信息查看");
		cmdMaping.put("3103", "用户信息编辑");
		cmdMaping.put("3104", "用户密码操作");
		cmdMaping.put("3105", "发送验证码");
		cmdMaping.put("3106", "校验验证码");
		cmdMaping.put("3107", "第三方联合登录");

		cmdMaping.put("3200", "用户账户详情接口");

		cmdMaping.put("3300", "投注接口");
		cmdMaping.put("3301", "期次查询接口");
		cmdMaping.put("3302", "场次查询");
		cmdMaping.put("3303", "投注查询");
		cmdMaping.put("3304", "投注撤销");
		cmdMaping.put("3306", "投注详情");
		cmdMaping.put("3307", "开奖大厅");
		cmdMaping.put("3308", "中奖查询");
		cmdMaping.put("3001", "版本检测");
		cmdMaping.put("4501", "比分直播");

		// func 跳转
		cmdFuncMaping.put("3201_listNew", "用户进出明细");
		cmdFuncMaping.put("3002_", "功能开关");
		cmdFuncMaping.put("3002_clientActivityConfig", "功能开关");
		cmdFuncMaping.put("3002_pageInfo", "页面信息列表");
		cmdFuncMaping.put("3002_mallPageInfo", "商城首页");
		cmdFuncMaping.put("3005_commonPush", "通用消息");
		cmdFuncMaping.put("3203_bank", "银行卡提现");
		cmdFuncMaping.put("3203_bankUni", "银行卡提现带银行明细");
		cmdFuncMaping.put("3203_alipay", "支付宝提现提现");

		cmdFuncMaping.put("3005_list", "消息中心列表");
		cmdFuncMaping.put("3005_read", "消息已读");
		cmdFuncMaping.put("3005_readAll", "消息全部已读");
		cmdFuncMaping.put("3005_unReadCount", "消息未读数量");
		cmdFuncMaping.put("3005_configure", "推送设置");
		cmdFuncMaping.put("3005_queryConfigure", "查询推送设置");

		/*-------邱凯添加---开始-------------*/
		cmdFuncMaping.put("3202_yinlian", "银联充值旧接口");
		cmdFuncMaping.put("3202_yinlian3", "银联充值");
		cmdFuncMaping.put("3004_sendCoupon", "抽奖调用的发红包接口");
		cmdFuncMaping.put("3004_couponCodes", "展示红包(优惠码,在私人消息中的)列表");
		cmdFuncMaping.put("3106_duiHuanYouHuiMaUni", "红包兑换新");
		cmdFuncMaping.put("3106_duiHuanYouHuiMa", "红包兑换旧（优惠码兑换）");
		/*-------邱凯添加---结束-------------*/

		cmdFuncMaping.put("3004_banner", "cms接口查询banner列表");
		cmdFuncMaping.put("3004_message", "调用cms接口查询列表公告和活动");
		
		/*吴修伟——添加：开始*/
		cmdFuncMaping.put("3003_list", "资讯列表");
		cmdFuncMaping.put("3003_detail", "资讯详情");
		cmdFuncMaping.put("3202_alipayIOS", "支付宝充值-iOS");
		/*吴修伟——添加：结束*/
		
		cmdFuncMaping.put("3000_clientActive", "客户端激活");
		cmdFuncMaping.put("3004_isNew", "接口判断是否有最新文章");
		cmdFuncMaping.put("3004_privateMessage", "获得私人消息列表");
		cmdFuncMaping.put("3004_noReadedCountPrivateMessage", "获得未读消息数量");
		cmdFuncMaping.put("3004_officalDoc", "文案接口");
		cmdFuncMaping.put("3004_oneDonateArticle", "用cms接口查询公益单篇文章");
		cmdFuncMaping.put("3004_oneArticle", "用cms接口查询单篇文章");
		cmdFuncMaping.put("3004_list", "公告列表");
		/*-------LN Add  strat-------------*/
		cmdFuncMaping.put("3004_fillType", "充值渠道清单");
		cmdFuncMaping.put("3004_donatePerson", "获得个人捐赠金额");
		cmdFuncMaping.put("3004_donateAllPerson", "获得所有人捐赠金额");
		cmdFuncMaping.put("3004_donateProject", "获得项目捐赠人次");
		cmdFuncMaping.put("3004_donateList", "公益项目列表");
		cmdFuncMaping.put("3004_donateBanner", "公益项目列表(banner)");
		/*-------LN Add  end-------------*/
		cmdMaping.put("3309", "获取大厅的滚动列表");
		
		/**shizhihui 20160630*/
		cmdMaping.put("3204", "反馈接口");
		cmdFuncMaping.put("3005_newProp", "新增道具"); 
		cmdFuncMaping.put("3000_", "客户端激活");
		/**shizhihui 20160630*/
		
		cmdFuncMaping.put("3005_propsExpire", "道具到期");
		cmdFuncMaping.put("3201_list", "用户进出明细");
		cmdFuncMaping.put("3201_", "用户进出明细");
		cmdFuncMaping.put("3004_detail", "公告详情");
		cmdFuncMaping.put("3203_list", "提现记录");
		
		cmdFuncMaping.put("3202_list", "充值记录");
		cmdFuncMaping.put("3202_", "充值记录");
		
		/**SHIZHIHUI 20160714*/
		cmdFuncMaping.put("3002_clientActivityWel", "欢迎页活动");//返回空
		cmdFuncMaping.put("3004_onePrivateMessage", "获得一条消息的具体内容");//返回升级客户端
		cmdFuncMaping.put("3202_bftPay", "帮付通充值(借记卡)");//返回升级客户端
		cmdFuncMaping.put("3202_hftx", "汇付天下充值(信用卡)");//返回升级客户端
		cmdFuncMaping.put("3202_weixin", "微信支付");//返回升级客户端
		cmdFuncMaping.put("3202_alipay", "支付宝充值");//返回升级客户端
		cmdFuncMaping.put("3202_present", "赠金充值");//返回失败
		/**SHIZHIHUI 20160714*/

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}
}
